package org.mao.forumtest;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.mao.forum.entity.ForumSection;
import org.mao.forum.service.ForumSectionService;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.google.common.collect.Lists;

//读入所有版块的url, 初始化数据用
public class ForumSectionInit {

	public List<ForumSection> read(String url) throws Exception {
		List<ForumSection> forumSectionList = Lists.newArrayList();
		Document doc = Jsoup.connect(url).timeout(1000).get();
		//Document doc = Jsoup.parse(new File("F:/contract/download/car.txt"), "UTF-8");
		Elements bigSections = doc.getElementsByClass("mainbox");
		for (Element bigSection : bigSections) {
			//System.out.println(bigSection.select("h3").text());
			Elements tbodys = bigSection.select(">table>tbody");
			for (Element tbody : tbodys) {
				String tbodyId = tbody.attr("id");
				Elements links = tbody.select(">tr>th>h2>a");
				if (links.size() > 0) {
					Element link = links.get(0);
					//System.out.println("----------" + link.text());
					ForumSection forumSection = new ForumSection();
					forumSection.setCode(StringUtils.isBlank(tbodyId) ? null : tbodyId);
					forumSection.setName(link.text());
					forumSection.setUrl(link.attr("abs:href"));
					forumSectionList.add(forumSection);
				}
			}
		}
		return forumSectionList;
	}

	public static void main(String[] arg) {
		try {
			AbstractApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath*:spring/spring-beans.xml", "classpath*:spring/spring-*.xml" });
			ForumSectionService forumSectionService = (ForumSectionService) context.getBean("forumSectionServiceImpl");
			System.out.println(forumSectionService.list().size());

			ForumSectionInit forumSectionInit = new ForumSectionInit();
			List<ForumSection> forumSectionList = forumSectionInit.read("http://www.baidu.com");
			//forumSectionService.batchInsert(forumSectionList, new Date());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.out.println("exist");
			System.exit(0);
		}
	}

}
